home *** CD-ROM | disk | FTP | other *** search
- Path: acs5.bu.edu!lachesis
- From: lachesis@bu.edu (wai yip)
- Newsgroups: comp.lang.c
- Subject: pointers and functions
- Date: 25 Mar 1996 15:23:01 GMT
- Organization: Boston University
- Message-ID: <4j6dol$58g@news.bu.edu>
- NNTP-Posting-Host: acs5.bu.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- can someone help me find the bug in this program?
- this is the file the program reads from:
- 0 1
- 5 1
- 4 1
- * ***
- * ***
- * ***
- * ***
-
- and here is the part of the program:
-
- #include <stdio.h>
-
- typedef struct{
- int W;
- int H;
- int Sr;
- int Sc;
- int Er;
- int Ec;
- int Max;
- char *mazePtr;
- }mazeType;
-
- mazeType *readMaze(char *fname);
- int printMaze(mazeType *maze);
-
-
- mazeType *readMaze(char *fname)
- {
- FILE *ifp;
- char c,*Array=NULL;
- int x=0;
- mazeType Maze;
- mazeType *StructPtr=NULL;
-
- ifp=fopen(fname,"r");
- fscanf(ifp,"%d%d",&Maze.W,&Maze.H);
- fscanf(ifp,"%d%d",&Maze.Sr,&Maze.Sc);
- fscanf(ifp,"%d%d",&Maze.Er,&Maze.Ec);
-
- if ((Array=(char *)malloc(sizeof(char)*Maze.W*Maze.H)) == NULL)
- exit(1);
- while ((c=getc(ifp)) != EOF)
- {
- Array[x]=c;
- x++;
- }
- printf("X=Max=%d\n",x);
- Maze.Max=x;
- Maze.mazePtr=Array;
- StructPtr=&Maze;
- return (StructPtr);
- }
-
- int printMaze(mazeType *maze)
- {
- int x;
-
- if (maze == NULL) return 0;
- printf("mazeWidth= %d mazeHeight= %d\n",maze->W,maze->H);
- printf("startRow= %d startColumn= %d\n",maze->Sr,maze->Sc);
- printf("endRow= %d endColumn= %d\n",maze->Er,maze->Ec);
- printf("maze size is: %d",maze->Max);
-
- for (x=0;x<maze->Max;x++)
- printf("%c",maze->mazePtr[x]);
- printf("\n");
-
- return 1;
- }
-
-
- void main(int argc,char *argv[])
- {
- mazeType *StructPtr=NULL;
-
- StructPtr=readMaze(argv[1]);
-
- if (!printMaze(StructPtr))
- {
- printf("Cannot print maze because it is NULL\n");
- exit (1);
- }
- }
-
- the problem with this program is that it doesn't print some of the data out
- correctly and doesn't print out the maze at all when passed to the function.
- It printed everything out correctly in main when i tried it, but not in the
- function... which leads me to believe i'm calling it incorrectly.
-